package com.bxx.ermall.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bxx.ermall.common.dto.CommentDto;
import com.bxx.ermall.common.lang.Result;
import com.bxx.ermall.entity.Comment;
import com.bxx.ermall.service.CommentService;
import com.bxx.ermall.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author buxiaoxing
 * @since 2021-02-02
 */
@RestController("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    //评论
    @PostMapping("/add")
    public Result add(@RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer goodsId = (Integer)jsonObject.get("goodsId");
        String content = (String)jsonObject.get("content");
        Integer grade = (Integer)jsonObject.get("grade");
        Comment temp = new Comment();
        temp.setGoodsId(goodsId);
        temp.setContent(content);
        temp.setGrade(grade);
        temp.setCreateTime(LocalDateTime.now());
        temp.setAuthor(ShiroUtil.getProfile().getId());
        commentService.save(temp);
        return Result.succ(null);
    }


    //回复
    @PostMapping("/reply")
    public Result reply(@RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        String reply = (String)jsonObject.get("reply");
        Comment item = commentService.getById(id);
        if(item.getReply()!=null){
            return Result.fail("不可多次回复");
        }
        item.setReply(reply);
        commentService.updateById(item);
        return Result.succ(null);
    }

    //追评
    @PostMapping("/review")
    public Result review(@RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        String review = (String)jsonObject.get("review");
        Comment item = commentService.getById(id);
        if(item.getSubComment()!=null){
            return Result.fail("不可多次追评");
        }
        item.setSubComment(review);
        commentService.updateById(item);
        return Result.succ(null);
    }

    //追回
    @PostMapping("/rereply")
    public Result rereply(@RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        String rereply = (String)jsonObject.get("rereply");
        Comment item = commentService.getById(id);
        if(item.getSubReply()!=null){
            return Result.fail("不可多次回复");
        }
        item.setSubReply(rereply);
        commentService.updateById(item);
        return Result.succ(null);
    }

    @GetMapping("/getByGoods")
    public Result getByGoods(@RequestParam Integer id){
        List<CommentDto> data = commentService.getGoodsComment(id);
        return Result.succ(data);
    }
//
    @GetMapping("/getByUser")
    public Result getByUser(){
        List<CommentDto> data = commentService.getUserComment(ShiroUtil.getProfile().getId());
        return Result.succ(data);
    }
}

